<template>
  <div class="app-container">
    <div class="content-container">
      <!--      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="keywords" label="姓名">
            <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable />
          </el-form-item>
          <el-form-item prop="keywords" label="联系方式">
            <el-input v-model="queryParams.mobile" placeholder="请输入联系方式" clearable />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>-->
      <el-card shadow="never" class="table-card">
        <div class="flex-x-between mb-10px">
          <div>
            <el-button type="success" icon="plus" @click="handleOpenDialog">新增</el-button>
          </div>
        </div>
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            :page-size="queryParams.pageSize"
            :page-num="queryParams.pageNum"
            @handleQuery="handlePageChange"
          >
            <template #operation="{ row, name }">
              <el-button link type="primary" icon="Edit" @click="handleEdit(row)">编辑</el-button>
              <el-button
                type="danger"
                size="small"
                link
                icon="delete"
                @click="handleDelete(row.id)"
              >
                删除
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <phrases-form ref="phrasesFormRef" @load-data="handleQuery"></phrases-form>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import PhrasesAPI, { MerchantPhrasesVO } from "@/api/phrases";
import PhrasesForm from "@/views/phrases/manage/components/phrases-form.vue";

const loading = ref(false);
const phrasesFormRef = ref();
const total = ref(0);
const queryFormRef = ref();

const queryParams = reactive<PageQuery>({
  pageNum: 1,
  pageSize: 20,
});
const handlePageChange = (page) => {
  queryParams.pageNum = page.pageNum;
  queryParams.pageSize = page.pageSize;
  handleQuery();
};
const tableData = ref<MerchantPhrasesVO[]>([]);
const tableColumnsData = ref([
  {
    prop: "content",
    label: "常用语",
    showOverflowTooltip: true,
  },
  {
    prop: "remark",
    label: "备注",
    showOverflowTooltip: true,
  },
  {
    prop: "operation",
    label: "操作",
    width: 150,
    align: "center",
    fixed: "right",
    customSlot: "operation", // 自定义插槽名称
  },
]);
// 查询
function handleQuery() {
  loading.value = true;
  console.log(queryParams, "queryParams");
  PhrasesAPI.page(queryParams)
    .then((data: any) => {
      tableData.value = data.data || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

function handleEdit(row: MerchantPhrasesVO) {
  phrasesFormRef.value.showModal(row);
}
async function handleOpenDialog() {
  phrasesFormRef.value.showModal({});
}

// 删除部门
async function handleDelete(id) {
  const result = await ElMessageBox.confirm("确认删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });
  if (!result) return;
  try {
    loading.value = true;

    await PhrasesAPI.delete(id);
    ElMessage.success("删除成功");
    handleQuery();
  } catch (e) {
    console.log(e);
  } finally {
    loading.value = false;
  }
}
onMounted(async () => {
  handleQuery();
});
</script>
